<?php
class RssController extends CController{
	public $layout = 'rss';
	public $menu;
	public function actionIndex(){
		$news = News::model()->findAll(
			array('order'=>'post_time DESC')
		);
		$this->render('restemplate',array('rss'=>$news));
	}
	public function actionView(){
		$this->layout = 'layout1';

		$this->menu=array(
						array('label'=>'发现', 'url'=>array('/news'),'linkOptions'=>array('class'=>'turn1'),'itemOptions'=>array('class'=>'nav_left')),
						array('label'=>'赞友', 'url'=>array('/upper'),'linkOptions'=>array('class'=>'turn2'),'itemOptions'=>array('class'=>'nav_mid')),
						array('label'=>'收藏', 'url'=>array('/favorites'),'linkOptions'=>array('class'=>'turn2'),'itemOptions'=>array('class'=>'nav_right')),
					);
		$this->pageTitle = '最近分享';
		$domain = NC::get_header();
		Yii::app()->clientScript->registerScriptfile('/js/nc.rss.js',CClientScript::POS_END);
		Yii::app()->clientScript->registerScript('domain',"var domain = '$domain';",CClientScript::POS_HEAD);
		Yii::app()->clientScript->registerCssFile('/css/logined.css');
		$userFavor = $upper = false;
		if(!Yii::app()->user->isGuest){
			$userFavor = User::model()->findByPk(Yii::app()->user->id);
			$upper = Upper::model()->findAll("user_id=:user_id order by type ASC,upper_time DESC",array(":user_id"=>Yii::app()->user->id));
		}
		$this->render('index',array('userFavor'=>$userFavor,'upper'=>$upper));
	}
	public function actionTag(){
		$tagName = trim(NC::_g('tag',''));
		$this->pageTitle = $tagName;
		if(!$tagName)
			$news = News::model()->findAll(
				array('order'=>'post_time DESC')
			);
		else{
			$tags = Tags::model()->findByAttributes(array('tag_name'=>$tagName));
			$newsIds = 0;
			if(!empty($tags)){
				$newsIdArray = json_decode($tags->news_ids);
				$newsIds = $newsIdArray->news_ids;
				$newsIds = implode(',',$newsIds);
			}
			if(strpos($newsIds,',')){
				$news = News::model()->findAll("news_id in($newsIds) order by post_time DESC");
			}else{
				$news = News::model()->findAll("news_id = $newsIds");
			}
		}
		$this->render('restemplate',array('rss'=>$news));
	}
	public function actionUser(){
		$userName = trim(NC::_g('user',''));
		$this->pageTitle = $userName;
		if(!$userName)
			$news = News::model()->findAll(
				array('order'=>'post_time DESC')
			);
		else{
			$news = News::model()->findAll("user_name = '$userName' order by post_time DESC");
		}
		$this->render('restemplate',array('rss'=>$news));
	}
}